package cn.itcast.service.company.impl;

import cn.hutool.core.util.IdUtil;
import cn.itcast.dao.company.CompanyDao;
import cn.itcast.domain.company.Company;
import cn.itcast.domain.vo.PageBean;
import cn.itcast.service.company.CompanyService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyDao companyDao;

    @Override
    public List<Company> findAll() {
        return companyDao.findAll();
    }

    @Override
    public Company findById(String id) {
        return companyDao.findById(id);
    }

    @Override
    public void save(Company company) {
        // 指定主键，使用uuid
        // company.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        // 使用hutool工具实现uuid生成
        company.setId(IdUtil.simpleUUID());
        companyDao.save(company);
    }

    @Override
    public void update(Company company) {
        companyDao.update(company);
    }

    @Override
    public void delete(String id) {
        companyDao.delete(id);
    }

    @Override
    public PageBean findByPage(Integer pageNum, Integer pageSize) {
        // 1.查询总记录数
        Long totalCount = companyDao.findCount();
        // 2.计算开始索引
        Integer index = (pageNum-1)*pageSize;

        // 3.查询list结果集
        List<Company> list = companyDao.findList(index,pageSize);

        // 4.封装并返回分页对象
        return new PageBean(pageNum, pageSize, totalCount, list);
    }

    @Override
    public PageInfo findByPageHelper(Integer pageNum, Integer pageSize) {

        // 1.开启分页插件功能
        PageHelper.startPage(pageNum, pageSize);
        // 2.查询所有，要求：查询语句必须在开启分页插件之后
        List<Company> list = companyDao.findAll();
        // 3.封装分页对象
        return new PageInfo(list,10); // 前五后四
    }
}
